package com.order.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.order.feign.ProducerService;
import com.order.feign.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
public class OrderContral {
    @Autowired
    RestTemplate restTemplate;



    @Autowired
    StockService stockService;
    @Autowired
    ProducerService producerService;

    @RequestMapping("/add")
    public String add() {
        System.out.println("下单成功");
        String deduct = restTemplate.getForObject("http://stock-service/stock/deduct", String.class);
        System.out.println(deduct);
        String produce = restTemplate.getForObject("http://producer-service/producer/produce", String.class);
        return "下单成功" + deduct+" "+produce;
    }

    @GetMapping ("/feignAdd")
    public String feignAdd() {
        String deduct = stockService.deduct();
        String produce = producerService.produce();
        return "下单成功：===" + deduct + "  " + produce;
    }
    @RequestMapping("/get/{id}")
    @SentinelResource(value = "getById",blockHandler = "HotBlockHandler")
    public String getById(@PathVariable("id")Integer id){

        System.out.println("正常访问");
        return "正常访问";
    }
    public String HotBlockHandler(@PathVariable("id")Integer id, BlockException e){

        return "热点异常处理";
    }


}
